import HDataTable from "@/components/ant-ext-table/HDataTable";
import {Form} from "antd";
import {DeleteOutlined, PlusSquareOutlined} from "@ant-design/icons";
import HAction from "@/components/common/HAction";
import React from "react";
import {HDataTableSlice} from "@/components/ant-ext-table/redux/dataTableSlice";
import {Dispatch} from "redux";
import {HTableProps} from "@/components/ant-ext-table/HTable";
import {useDispatch} from "react-redux";
import EntityConfig from "@/api/entity-config";
import {ConfigType} from "@/const/const";
import HTag from "@/components/ant-ext/HTag";
import ConfigEdit from "@/pages/m-config/edit";

function getColumns(dispatch: Dispatch): HTableProps<any>["columns"] {
    return [
        {
            title: "ID",
            dataIndex: 'id',
            width: '200px',
            fixed: 'left',
        }, {
            title: "配置标识",
            dataIndex: 'label',
        }, {
            title: "配置名称",
            dataIndex: 'labelName',
            render: (value, record) => {
                return <HAction type={"link"}
                                empower={EntityConfig.EDIT}
                                invalid
                                action={() => dispatch!(HDataTableSlice.actions.doAction({
                                    action: EntityConfig.EDIT,
                                    rowKey: EntityConfig.RowKey,
                                    row: record
                                }))}
                                children={value}/>
            }
        }, {
            title: "类型",
            dataIndex: 'type',
            render: (v) => <HTag value={v} mapping={ConfigType.tag}/>
        }
    ]
}

export default function Index() {
    const [form] = Form.useForm()
    const dispatch = useDispatch()

    return <>
        <ConfigEdit/>
        <HDataTable
            option={[
                {key: EntityConfig.ADD, title: "新增", icon: <PlusSquareOutlined/>},
                {
                    key: EntityConfig.DEL,
                    title: "删除",
                    mode: "multi",
                    icon: <DeleteOutlined style={{color: "red"}}/>,
                    confirm: "是否要删除所选数据",
                    action: (data) => {
                        EntityConfig.delete({
                            body: {idList: data.selectRowKeys},
                            success: () => dispatch(HDataTableSlice.actions.setQueryParams({}))
                        })
                    }
                },
            ]}
            queryForm={{
                form: form,
                item: [
                    {name: "labelName", label: "配置名称"},
                    {name: "type", label: "配置类型", type: "select", dataSource: ConfigType.select, width: "150px"},
                ]
            }}
            columns={getColumns(dispatch)} selectMethod={EntityConfig.selectTablePage}
            rowKey={"id"}
        />
    </>
}